<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/72X72.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/48X48.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/32X32.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"issacyoung.cn","root":"/","scheme":"Pisces","version":"7.7.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="为什么测试？  整理编码思路 增加对项目的理解  为什么做单元测试？  省时省力，及早发现bug 方便重构  为什么不做单元测试？  难以理解、维护，业务代码的单测代码量可能比业务代码本身还多 依赖难以去除，数据库、微服务依赖   Spock简介Spock国外一款优秀的测试框架，基于BDD（行为驱动开发）思想，结合Groovy动态语言，简洁、高效、层次清晰，IDE兼容好，自带Mock功能。相比J">
<meta property="og:type" content="article">
<meta property="og:title" content="一步一步之Spock">
<meta property="og:url" content="http://issacyoung.cn/2021/09/15/%E4%B8%80%E6%AD%A5%E4%B8%80%E6%AD%A5%E4%B9%8BSpock/index.html">
<meta property="og:site_name" content="Young‘s River">
<meta property="og:description" content="为什么测试？  整理编码思路 增加对项目的理解  为什么做单元测试？  省时省力，及早发现bug 方便重构  为什么不做单元测试？  难以理解、维护，业务代码的单测代码量可能比业务代码本身还多 依赖难以去除，数据库、微服务依赖   Spock简介Spock国外一款优秀的测试框架，基于BDD（行为驱动开发）思想，结合Groovy动态语言，简洁、高效、层次清晰，IDE兼容好，自带Mock功能。相比J">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-09-15T07:24:57.000Z">
<meta property="article:modified_time" content="2021-10-13T08:33:04.430Z">
<meta property="article:author" content="Issac Young">
<meta property="article:tag" content="单元测试">
<meta property="article:tag" content="Groovy">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://issacyoung.cn/2021/09/15/%E4%B8%80%E6%AD%A5%E4%B8%80%E6%AD%A5%E4%B9%8BSpock/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true
  };
</script>

  <title>一步一步之Spock | Young‘s River</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="Young‘s River" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Young‘s River</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <p class="site-subtitle">一个正能量的程序员，喜欢码字，热爱运动，向往自由</p>
  </div>

  <div class="site-nav-right"></div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
  </ul>

</nav>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

  <a href="https://github.com/IssacYoung2013" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://issacyoung.cn/2021/09/15/%E4%B8%80%E6%AD%A5%E4%B8%80%E6%AD%A5%E4%B9%8BSpock/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/author.jpg">
      <meta itemprop="name" content="Issac Young">
      <meta itemprop="description" content="现代生活工作节奏越来越快，需要有时间和空间沉淀积累，将知识点规划成树，记录自己的感悟心得。">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Young‘s River">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          一步一步之Spock
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-09-15 15:24:57" itemprop="dateCreated datePublished" datetime="2021-09-15T15:24:57+08:00">2021-09-15</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-10-13 16:33:04" itemprop="dateModified" datetime="2021-10-13T16:33:04+08:00">2021-10-13</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Java/" itemprop="url" rel="index">
                    <span itemprop="name">Java</span>
                  </a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>5.8k</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <blockquote>
<p>为什么测试？</p>
<ol>
<li>整理编码思路</li>
<li>增加对项目的理解</li>
</ol>
<p>为什么做单元测试？</p>
<ol>
<li>省时省力，及早发现bug</li>
<li>方便重构</li>
</ol>
<p>为什么不做单元测试？</p>
<ol>
<li>难以理解、维护，业务代码的单测代码量可能比业务代码本身还多</li>
<li>依赖难以去除，数据库、微服务依赖</li>
</ol>
</blockquote>
<h2 id="Spock简介"><a href="#Spock简介" class="headerlink" title="Spock简介"></a>Spock简介</h2><p>Spock国外一款优秀的测试框架，基于BDD（行为驱动开发）思想，结合Groovy动态语言，简洁、高效、层次清晰，IDE兼容好，自带Mock功能。相比JUnit、Mockito，使用起来更简单。</p>
<h4 id="Groovy"><a href="#Groovy" class="headerlink" title="Groovy"></a>Groovy</h4><ul>
<li>扩展java为目的的语言</li>
<li>语法精简、表达性强</li>
<li>典型应用：jenkins、elasticsearch、gradle</li>
</ul>
<h3 id="为什么是Spock"><a href="#为什么是Spock" class="headerlink" title="为什么是Spock"></a>为什么是Spock</h3><h4 id="优点"><a href="#优点" class="headerlink" title="优点"></a>优点</h4><ul>
<li>遵循BDD模式、功能强大、语义规范、可读性强、易于维护</li>
<li>描述测试步骤用自然语言描述 give 、when、then、expect</li>
<li>兼容mock</li>
<li>习惯上单元测试，提升代码质量的利器</li>
</ul>
<h4 id="缺点"><a href="#缺点" class="headerlink" title="缺点"></a>缺点</h4><ul>
<li>groovy 的学习成本</li>
<li>不支持静态方法mock，需要引入PowerMockito</li>
</ul>
<h3 id="Spock中的概念"><a href="#Spock中的概念" class="headerlink" title="Spock中的概念"></a>Spock中的概念</h3><h4 id="Specification"><a href="#Specification" class="headerlink" title="Specification"></a>Specification</h4><p>字面意思是规范、规格，测试类需要继承Specification类</p>
<h4 id="Fields"><a href="#Fields" class="headerlink" title="Fields"></a>Fields</h4><p>Specification类中可以定义字段，会在每个测试方法前重新初始化</p>
<h4 id="Fixed-Methods"><a href="#Fixed-Methods" class="headerlink" title="Fixed Methods"></a>Fixed Methods</h4><p>固定函数，类似junit中的</p>
<figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> setup() &#123;&#125; <span class="comment">// 每个测试方法前执行</span></span><br><span class="line"><span class="keyword">def</span> cleanup() &#123;&#125; <span class="comment">// 每个测试方法后执行</span></span><br><span class="line"><span class="keyword">def</span> setupSpec() &#123;&#125; <span class="comment">// 第一个测试方法前执行</span></span><br><span class="line"><span class="keyword">def</span> cleanupSpec() &#123;&#125; <span class="comment">// 最后一个测试方法后执行</span></span><br></pre></td></tr></table></figure>

<h4 id="Blocks"><a href="#Blocks" class="headerlink" title="Blocks"></a>Blocks</h4><p>每个测试方法划分为不同的block，不同的block表示测试处于不同的阶段</p>
<ul>
<li>setup / given 测试函数初始化</li>
<li>when / then 需要搭配使用，when 中执行待测试函数，then 中判断是否符合预期</li>
<li>expect 精简版的 when / then</li>
<li>thrown 验证异常</li>
<li>where 测试数据</li>
</ul>
<h2 id="Spock使用"><a href="#Spock使用" class="headerlink" title="Spock使用"></a>Spock使用</h2><h3 id="添加依赖"><a href="#添加依赖" class="headerlink" title="添加依赖"></a>添加依赖</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-test<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">scope</span>&gt;</span>test<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--引入spock 核心包--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.spockframework<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spock-core<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.3-groovy-2.5<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">scope</span>&gt;</span>test<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--引入spock 与 spring 集成包--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.spockframework<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spock-spring<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.3-groovy-2.5<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">scope</span>&gt;</span>test<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--引入 groovy 依赖--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.codehaus.groovy<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>groovy-all<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.5.7<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">scope</span>&gt;</span>test<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="基础语法"><a href="#基础语法" class="headerlink" title="基础语法"></a>基础语法</h3><blockquote>
<p>对于springboot项目，groovy测试类的文件必须创建在 test/groovy 下，而不是 test/java，文件类型是 Groovy文件</p>
</blockquote>
<ul>
<li>given 定义前置条件</li>
<li>and 其他前置条件</li>
<li>when 当发生一个特定事件</li>
<li>then 产生的后置结果</li>
<li>expect 随处可用的断言</li>
<li>with 判断对象</li>
<li>where 测试数据</li>
<li>setup 单个测试方法前置</li>
<li>setupSpec 整个测试环境前置</li>
<li>cleanup 单个测试方法后置</li>
<li>cleanupSpec 整个测试环境后置</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Title</span>(<span class="string">"标题：计算"</span>)</span><br><span class="line"><span class="meta">@Narrative</span>(<span class="string">"Spock单测示例"</span>)</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">CalculatorTest</span> <span class="keyword">extends</span> <span class="title">Specification</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function">def <span class="title">setup</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        println <span class="string">"=====方法测试前置===="</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function">def <span class="title">cleanup</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        println <span class="string">"=====方法测试后置===="</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function">def <span class="title">setupSpec</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        println <span class="string">"=====整个测试环境前置===="</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function">def <span class="title">cleanupSpec</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        println <span class="string">"=====整个测试环境后置===="</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    def <span class="string">"test add"</span>() &#123;</span><br><span class="line">        expect:</span><br><span class="line">        Calculator.add(<span class="number">1</span>, <span class="number">1</span>) == <span class="number">2</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    def <span class="string">"test and given"</span>() &#123;</span><br><span class="line">        given:</span><br><span class="line">        def num1 = <span class="number">1</span></span><br><span class="line">        def num2 = <span class="number">1</span></span><br><span class="line"></span><br><span class="line">        and:</span><br><span class="line">        def result = <span class="number">2</span></span><br><span class="line"></span><br><span class="line">        expect:</span><br><span class="line">        Calculator.add(num1, num2) == result;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 表示where下面的每一行作为一个单独的测试用例</span></span><br><span class="line">    <span class="meta">@Unroll</span></span><br><span class="line">    def <span class="string">"test add with expect where"</span>() &#123;</span><br><span class="line">        expect:</span><br><span class="line">        Calculator.add(num1, num2) == result</span><br><span class="line"></span><br><span class="line">        where:</span><br><span class="line">        num1 | num2 | result</span><br><span class="line">        <span class="number">1</span>    | <span class="number">1</span>    | <span class="number">2</span></span><br><span class="line">        <span class="number">1</span>    | <span class="number">2</span>    | <span class="number">3</span></span><br><span class="line">        <span class="number">1</span>    | <span class="number">3</span>    | <span class="number">4</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// #num1动态替换成字面量</span></span><br><span class="line">    <span class="meta">@Unroll</span></span><br><span class="line">    def <span class="string">"test add with expect where unroll arr by #num1 + #num2 = #result"</span>() &#123;</span><br><span class="line">        expect:</span><br><span class="line">        Calculator.add(num1, num2) == result</span><br><span class="line"></span><br><span class="line">        where:</span><br><span class="line">        num1 &lt;&lt; [<span class="number">1</span>, <span class="number">1</span>, <span class="number">2</span>]</span><br><span class="line">        num2 &lt;&lt; [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line">        result &lt;&lt; [<span class="number">2</span>, <span class="number">3</span>, <span class="number">5</span>]</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Unroll</span></span><br><span class="line">    def <span class="string">"test int dived zero exception"</span>() &#123;</span><br><span class="line">        when:</span><br><span class="line">        Calculator.divideInt(<span class="number">1</span>, <span class="number">0</span>)</span><br><span class="line"></span><br><span class="line">        then:</span><br><span class="line">        def ex = thrown(ArithmeticException)</span><br><span class="line">        ex.message == <span class="string">"/ by zero"</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Timeout</span>(value = <span class="number">900</span>, unit = TimeUnit.MILLISECONDS)</span><br><span class="line">    def <span class="string">"test timeout"</span>() &#123;</span><br><span class="line">        expect:</span><br><span class="line">        Thread.sleep(<span class="number">500</span>)</span><br><span class="line">        <span class="number">1</span> == <span class="number">1</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Ignore</span>(<span class="string">"忽略这个测试方法"</span>)</span><br><span class="line">    def <span class="string">"测试方法1"</span>() &#123;</span><br><span class="line">        given: <span class="string">"给定一个前置条件"</span></span><br><span class="line">        <span class="comment">//<span class="doctag">TODO:</span> code here</span></span><br><span class="line">        and: <span class="string">"其他前置条件"</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        expect: <span class="string">"随处可用的断言"</span></span><br><span class="line">        <span class="comment">//<span class="doctag">TODO:</span> code here</span></span><br><span class="line">        when: <span class="string">"当发生一个特定的事件"</span></span><br><span class="line">        <span class="comment">//<span class="doctag">TODO:</span> code here</span></span><br><span class="line">        and: <span class="string">"其他的触发条件"</span></span><br><span class="line"></span><br><span class="line">        then: <span class="string">"产生的后置结果"</span></span><br><span class="line">        <span class="comment">//<span class="doctag">TODO:</span> code here</span></span><br><span class="line">        and: <span class="string">"同时产生的其他结果"</span></span><br><span class="line"></span><br><span class="line">        where: <span class="string">"不是必需的测试数据"</span></span><br><span class="line">        input1 | input2 || output</span><br><span class="line">        <span class="number">111</span>    | <span class="number">222</span>    || <span class="number">333</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@IgnoreRest</span></span><br><span class="line">    <span class="comment">//只测试这个方法，而忽略所有其他方法</span></span><br><span class="line">    def <span class="string">"测试方法2"</span>() &#123;</span><br><span class="line">        <span class="comment">//<span class="doctag">TODO:</span> code here</span></span><br><span class="line">        expect:</span><br><span class="line">        <span class="number">1</span> == <span class="number">1</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Mock"><a href="#Mock" class="headerlink" title="Mock"></a>Mock</h3><p>mock的好处在于，单个类依赖的下游类可以mock，方便单元测试，也会有各式的问题。</p>
<h4 id="动态mock"><a href="#动态mock" class="headerlink" title="动态mock"></a>动态mock</h4><p>对于平常的业务开发，分层一般是 Serivce、DAO层，如果单测Service的代码，DAO的方法就可以mock，不必依赖数据库就可以单测。下面以用户登录为例，</p>
<p><code>UserDAO.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserDao</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 模拟数据库</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> Map&lt;String, User&gt; userMap = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> &#123;</span><br><span class="line">        userMap.put(<span class="string">"k"</span>, <span class="keyword">new</span> User(<span class="string">"k"</span>, <span class="number">1</span>, <span class="string">"123"</span>));</span><br><span class="line">        userMap.put(<span class="string">"i"</span>, <span class="keyword">new</span> User(<span class="string">"i"</span>, <span class="number">2</span>, <span class="string">"456"</span>));</span><br><span class="line">        userMap.put(<span class="string">"w"</span>, <span class="keyword">new</span> User(<span class="string">"w"</span>, <span class="number">3</span>, <span class="string">"789"</span>));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 通过用户名查询用户</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> name</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> User <span class="title">findByName</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> userMap.get(name);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><code>UserService.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserService</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> UserDao userDao;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> User <span class="title">findByName</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> userDao.findByName(name);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">loginAfter</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"登录成功"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">login</span><span class="params">(String name, String passwd)</span> </span>&#123;</span><br><span class="line">        User user = findByName(name);</span><br><span class="line">        <span class="keyword">if</span> (user == <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(name + <span class="string">"不存在"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (!user.getPasswd().equals(passwd)) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(name + <span class="string">"密码输入错误"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        loginAfter();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><code>UserSericeUnitTest.groovy</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserServiceUnitTest</span> <span class="keyword">extends</span> <span class="title">Specification</span> </span>&#123;</span><br><span class="line">    UserService userService = <span class="keyword">new</span> UserService()</span><br><span class="line">    UserDao userDao = Mock(UserDao)</span><br><span class="line"></span><br><span class="line">    <span class="function">def <span class="title">setup</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        userService.userDao = userDao;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    def <span class="string">"test login with success"</span>() &#123;</span><br><span class="line">        when:</span><br><span class="line">        userService.login(<span class="string">"k"</span>, <span class="string">"p"</span>)</span><br><span class="line"></span><br><span class="line">        then:</span><br><span class="line">        <span class="number">1</span> * userDao.findByName(<span class="string">"k"</span>) &gt;&gt; <span class="keyword">new</span> User(<span class="string">"k"</span>, <span class="number">12</span>, <span class="string">"p"</span>)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    def <span class="string">"test login with error"</span>() &#123;</span><br><span class="line">        given:</span><br><span class="line">        def name = <span class="string">"k"</span></span><br><span class="line">        def passwd = <span class="string">"p"</span></span><br><span class="line"></span><br><span class="line">        when:</span><br><span class="line">        userService.login(name, passwd)</span><br><span class="line"></span><br><span class="line">        then:</span><br><span class="line">        <span class="number">1</span> * userDao.findByName(name) &gt;&gt; <span class="keyword">null</span></span><br><span class="line"></span><br><span class="line">        then:</span><br><span class="line">        def e = thrown(RuntimeException)</span><br><span class="line">        e.message == <span class="string">"$&#123;name&#125;不存在"</span></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Unroll</span></span><br><span class="line">    def <span class="string">"test login with "</span>() &#123;</span><br><span class="line">        when:</span><br><span class="line">        userService.login(name, passwd)</span><br><span class="line"></span><br><span class="line">        then:</span><br><span class="line">        userDao.findByName(<span class="string">"k"</span>) &gt;&gt; <span class="keyword">null</span></span><br><span class="line">        userDao.findByName(<span class="string">"k1"</span>) &gt;&gt; <span class="keyword">new</span> User(<span class="string">"k1"</span>, <span class="number">12</span>, <span class="string">"p"</span>)</span><br><span class="line"></span><br><span class="line">        then:</span><br><span class="line">        def e = thrown(RuntimeException)</span><br><span class="line">        e.message == errMsg</span><br><span class="line"></span><br><span class="line">        where:</span><br><span class="line">        name | passwd | errMsg</span><br><span class="line">        <span class="string">"k"</span>  | <span class="string">"k"</span>    | <span class="string">"$&#123;name&#125;不存在"</span></span><br><span class="line">        <span class="string">"k1"</span> | <span class="string">"p1"</span>   | <span class="string">"$&#123;name&#125;密码输入错误"</span></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    def <span class="string">"test findByName by verity"</span>() &#123;</span><br><span class="line">        given:</span><br><span class="line">        def userDao = Mock(UserDao)</span><br><span class="line"></span><br><span class="line">        when:</span><br><span class="line">        userDao.findByName(<span class="string">"kk"</span>) &gt;&gt; <span class="keyword">new</span> User(<span class="string">"kk"</span>, <span class="number">12</span>, <span class="string">"33"</span>)</span><br><span class="line"></span><br><span class="line">        then:</span><br><span class="line">        def user = userDao.findByName(<span class="string">"kk"</span>)</span><br><span class="line">        with(user) &#123;</span><br><span class="line">            name == <span class="string">"kk"</span></span><br><span class="line">            age == <span class="number">12</span></span><br><span class="line">            passwd == <span class="string">"33"</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="抽象类、静态类方法mock"><a href="#抽象类、静态类方法mock" class="headerlink" title="抽象类、静态类方法mock"></a>抽象类、静态类方法mock</h4><p>借用PowerMockito</p>

    </div>

    
    
    
<div>
  
    <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

  
</div>
        <div class="reward-container">
  <div>辛苦码字分享，您的支持将鼓励我继续创作！</div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/wechatpay.jpg" alt="Issac Young 微信支付">
        <p>微信支付</p>
      </div>
      
      <div style="display: inline-block;">
        <img src="/images/alipay.jpg" alt="Issac Young 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Issac Young
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://issacyoung.cn/2021/09/15/%E4%B8%80%E6%AD%A5%E4%B8%80%E6%AD%A5%E4%B9%8BSpock/" title="一步一步之Spock">http://issacyoung.cn/2021/09/15/一步一步之Spock/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fa fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

            <div class="social-item">
              <a target="_blank" class="social-link" href="/atom.xml">
                <span class="icon">
                  <i class="fa fa-rss"></i>
                </span>

                <span class="label">RSS</span>
              </a>
            </div>
    </div>
  </div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95/" rel="tag"><i class="fa fa-tag"></i># 单元测试</a>
              <a href="/tags/Groovy/" rel="tag"><i class="fa fa-tag"></i># Groovy</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/07/17/%E4%B8%80%E6%AD%A5%E4%B8%80%E6%AD%A5%E4%B9%8B%E7%BA%BF%E7%A8%8B%E6%B1%A0/" rel="prev" title="一步一步之线程池">
      <i class="fa fa-chevron-left"></i> 一步一步之线程池
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/11/07/%E4%B8%80%E6%AD%A5%E4%B8%80%E6%AD%A5%E4%B9%8BSonar%E5%B9%B3%E5%8F%B0%E6%90%AD%E5%BB%BA/" rel="next" title="一步一步之Sonar平台搭建">
      一步一步之Sonar平台搭建 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let activeClass = CONFIG.comments.activeClass;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Spock简介"><span class="nav-number">1.</span> <span class="nav-text">Spock简介</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Groovy"><span class="nav-number">1.0.1.</span> <span class="nav-text">Groovy</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#为什么是Spock"><span class="nav-number">1.1.</span> <span class="nav-text">为什么是Spock</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#优点"><span class="nav-number">1.1.1.</span> <span class="nav-text">优点</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#缺点"><span class="nav-number">1.1.2.</span> <span class="nav-text">缺点</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Spock中的概念"><span class="nav-number">1.2.</span> <span class="nav-text">Spock中的概念</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Specification"><span class="nav-number">1.2.1.</span> <span class="nav-text">Specification</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Fields"><span class="nav-number">1.2.2.</span> <span class="nav-text">Fields</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Fixed-Methods"><span class="nav-number">1.2.3.</span> <span class="nav-text">Fixed Methods</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Blocks"><span class="nav-number">1.2.4.</span> <span class="nav-text">Blocks</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Spock使用"><span class="nav-number">2.</span> <span class="nav-text">Spock使用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#添加依赖"><span class="nav-number">2.1.</span> <span class="nav-text">添加依赖</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#基础语法"><span class="nav-number">2.2.</span> <span class="nav-text">基础语法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Mock"><span class="nav-number">2.3.</span> <span class="nav-text">Mock</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#动态mock"><span class="nav-number">2.3.1.</span> <span class="nav-text">动态mock</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#抽象类、静态类方法mock"><span class="nav-number">2.3.2.</span> <span class="nav-text">抽象类、静态类方法mock</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Issac Young"
      src="/images/author.jpg">
  <p class="site-author-name" itemprop="name">Issac Young</p>
  <div class="site-description" itemprop="description">现代生活工作节奏越来越快，需要有时间和空间沉淀积累，将知识点规划成树，记录自己的感悟心得。</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">26</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">19</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/IssacYoung2013" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;IssacYoung2013" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:issacyoung@msn.cn" title="E-Mail → mailto:issacyoung@msn.cn" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<div class="copyright">
  
  &copy; 2020 – 
  <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Issac Young</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">64k</span>
</div>


        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/clicklove.js"></script>

